
<?php
include "global.php";

if (isset($_POST["forget_essid"])){
    ////forget a credential
    $forget_essid = $_POST["forget_essid"];
    $path = config_credential_path();
    $credential_list = load_xml_from_file($path);
    //search entry and remove it
    foreach ($credential_list as $cred){
        if ($cred->ssid == $forget_essid){
            $dom=dom_import_simplexml($cred);
            $dom->parentNode->removeChild($dom );
            break;
        }
    }
    //save to XML file
    save_SimpleXMLElement_to_file($credential_list, $path);
}else if (isset($_POST["save_essid"])){
    //register new credential
    $save_essid = $_POST["save_essid"];
    $save_pass = $_POST["save_password"];
    $path = config_credential_path();
    $credential_list = load_xml_from_file($path);
    $found = false;
    //search if already such ESSID already registered
    foreach ($credential_list as $cred){
        if ($cred->ssid == $save_essid){
            //...if so update password with new provided
            $cred->pass = $save_pass;
            $found = true;
            break;
        }
    }
    if (!$found){
        //...else create new entry
        $item = $credential_list->addChild("item");
        $item->addChild("ssid", $save_essid);
        $item->addChild("pass", $save_pass);
        
    }
    //save to XML file
    save_SimpleXMLElement_to_file($credential_list, $path);
}
header('Location: index.php');
?>


